﻿using System;
using System.Linq;
using FluentValidation;

namespace LINQPad.OpenAccess.UI.ViewModel.Validators
{
    /// <summary>
    /// Validator for <see cref="ConnectionSettingsHostViewModel"/> instances
    /// </summary>
    public class ConnectionSettingsHostViewModelValidator : AbstractValidator<ConnectionSettingsHostViewModel>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionSettingsHostViewModelValidator" /> class.
        /// </summary>
        /// <param name="factoryBasedValidator">The factory based validator.</param>
        public ConnectionSettingsHostViewModelValidator( FactoryBasedValidator factoryBasedValidator )
        {
            // selected backend
            this.RuleFor( b => b.ConnectionSettings )
                .NotNull( )
                .WithMessage( "no connection settings set !" )
                .When( b => b.ConnectionSettingsAreRequired );

            this.RuleFor( b => b.ConnectionSettings )
                .SetValidator( factoryBasedValidator )
                .WithMessage( "connection settings invalid !" )
                .When( b => b.ConnectionSettings != null && b.ConnectionSettingsAreRequired );
        }
    }
}
